package cn.gcjxsc.service.integral;

import cn.gcjxsc.entity.salesman.RecommendMember;
import cn.gcjxsc.entity.salesman.Salesman;
import cn.gcjxsc.entity.salesman.SalesmanRecMemberStatics;
import cn.gcjxsc.repository.salesaman.RecommendMemberRepository;
import cn.gcjxsc.repository.salesaman.SalesmanRecMemberStaticsRepository;
import cn.gcjxsc.repository.specs.SalesmanSpec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 推荐会员业务类
 * @author pumbaa
 * @since 1.0
 */
@Service
public class RecommendMemberService {
    @Autowired
    private RecommendMemberRepository recommendMemberRepository;
    @Autowired
    private SalesmanRecMemberStaticsRepository staticsRepository;

    /**
     * 查询指定业务员的推荐会员
     * @param refMemberId 推荐会员id
     * @return
     */
    public List<RecommendMember> findByRefMemberId(Integer refMemberId, int page, int pageSize) {
        RecommendMember temp = new RecommendMember();
        temp.setSalesmanMemberId(refMemberId);
        Example<RecommendMember> example = Example.of(temp);
        PageRequest pageRequest = new PageRequest(page - 1, pageSize);
        Page<RecommendMember> pageList = recommendMemberRepository.findAll(example, pageRequest);
        return pageList.getContent();
    }

    /**
     * 查询指定业务员的推荐会员收益汇总
     * @param memberIdList 推荐会员id集
     * @param salesmanMemberId 业务员对应的会员id
     * @return
     */
    public List<SalesmanRecMemberStatics> findRefMemberStatics(List<Integer> memberIdList, Integer salesmanMemberId) {
        return staticsRepository.findBySalesmanMemberIdAndMemberIdIn(salesmanMemberId, memberIdList);
    }

    /**
     * 查找会员的推荐关系
     * @param memberId 会员id
     * @return
     */
    public RecommendMember findByMemberId(Integer memberId) {
        return recommendMemberRepository.findByMemberId(memberId);
    }

    /**
     *
     * @param memberId 会员id
     * @param refMemberId 业务员会员id
     * @return
     */
    public RecommendMember findByMemberId(Integer memberId, Integer refMemberId) {
        return recommendMemberRepository.findByMemberIdAndSalesmanMemberId(memberId,refMemberId);
    }
    /**
     *
     * @param memberId 会员id
     * @param refMemberId 业务员会员id
     * @return
     */
    public RecommendMember findByMemberIdAndSalesmanMemberIdAndStoreId(Integer memberId, Integer refMemberId, Integer storeId
    ) {
        return recommendMemberRepository.findByMemberIdAndSalesmanMemberIdAndStoreId(memberId,refMemberId, storeId);
    }


    /**
     *
     * @param memberId
     * @param storeId
     * @return
     */
    public RecommendMember findByMemberAndStoreId(Integer memberId, Integer storeId) {
        return recommendMemberRepository.findByMemberIdAndStoreId(memberId, storeId);
    }

    /**
     * 查询购物订单对应的会员的推荐关系,只查询最早绑定的那个业务员信息.
     *
     * @param memberId
     * @param storeId
     * @return
     */
    public RecommendMember findRelationship(Integer memberId, Integer storeId){
        Pageable pageRequest = new PageRequest(0, 1, new Sort(Sort.Direction.ASC, "created"));
        Page<RecommendMember> all = recommendMemberRepository.findAll(SalesmanSpec.buildRecommendMemberFilter(memberId, storeId), pageRequest);
        List<RecommendMember> content = all.getContent();
        return content.size() > 0 ? content.get(0) : null;
    }

    /**
     * 添加推荐关系
     * @param recommendMember 推荐关系
     */
    public void addRecommendMember(RecommendMember recommendMember) {
        recommendMemberRepository.save(recommendMember);
    }

    /**
     * 查询指定会员与业务员是否已存在有推荐关联关系
     * @param salesman 业务员
     * @param memberId 会员id
     * @return
     */
    public boolean isSalesmanRecommendMember(Salesman salesman, Integer memberId){
        return recommendMemberRepository.countBySalesmanIdAndMemberId(salesman.getId(), memberId) > 0;
    }
}
